package DesignPattern.Builder.Singleton;

import Utils.Util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Serialize {
    public static void main(String[] args) throws Exception {
//        writeObject2File();

        // 不同地址，破坏了单例方式
        readObject2File();
        Util.divide();
        readObject2File();

    }

    //   向文件中写数据
    public static void writeObject2File() throws Exception {
        TestSingleton testSingleton = TestSingleton.getInstance();

        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("src/main/java/DesignPattern/Builder/Singleton/FileSave/a.txt"));

        objectOutputStream.writeObject(testSingleton);

        objectOutputStream.close();
    }

    //   向文件中读数据
    public static void readObject2File() throws Exception {
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("DesignPattern/Builder/Singleton/FileSave/a.txt"));
        TestSingleton instance = (TestSingleton) objectInputStream.readObject();
        System.out.println(instance);
        objectInputStream.close();
    }
}
